#!/bin/bash

# 卸载 Docker 和 Kubernetes
uninstall_docker_and_k8s() {
    echo "开始卸载 Docker 和 Kubernetes..."
    # 停止并禁用 kubelet 服务
    systemctl stop kubelet
    systemctl disable kubelet
    # 停止并禁用 Docker 服务
    systemctl stop docker
    systemctl disable docker
    # 卸载 Docker 相关组件
    yum remove -y docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine \
                  docker-ce \
                  docker-ce-cli \
                  containerd.io
    # 卸载 Kubernetes 组件
    yum remove -y kubelet kubeadm kubectl
    # 删除相关配置和数据目录
    rm -rf /etc/kubernetes /var/lib/kubelet /var/lib/etcd /var/lib/dockershim /var/run/kubernetes ~/.kube
    # 清理 iptables 规则
    iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
    # 清理 ipvsadm 规则
    if command -v ipvsadm &> /dev/null; then
        ipvsadm -C
    fi
    echo "Docker 和 Kubernetes 卸载完成。"
}

# 环境准备
prepare_environment() {
    echo "开始环境准备..."
    # 关闭防火墙
    systemctl stop firewalld
    systemctl disable firewalld
    # 关闭 SELinux
    setenforce 0
    sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
    # 关闭交换空间
    swapoff -a
    sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
    # 加载内核模块
    modprobe br_netfilter
    cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
    sysctl --system
    echo "环境准备完成。"
}

# 安装 Docker
install_docker() {
    echo "开始安装 Docker..."
    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum install -y docker-ce docker-ce-cli containerd.io
    systemctl start docker
    systemctl enable docker
    echo "Docker 安装完成。"
}

# 安装 Kubernetes 组件
install_kubernetes() {
    echo "开始安装 Kubernetes 组件..."
    cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
    yum install -y kubelet kubeadm kubectl
    systemctl enable kubelet
    systemctl start kubelet
    echo "Kubernetes 组件安装完成。"
}

# 初始化 Kubernetes 集群
init_kubernetes_cluster() {
    echo "开始初始化 Kubernetes 集群..."
    # Check and start containerd service
    if ! systemctl is-active --quiet containerd; then
         yum install -y  containerd
        sudo systemctl start containerd
        sudo systemctl enable containerd
    fi
    kubeadm init --apiserver-advertise-address=$(hostname -i) --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.27.6 --pod-network-cidr=10.244.0.0/16
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    echo "Kubernetes 集群初始化完成。"
}

# 安装网络插件
install_network_plugin() {
    echo "开始安装网络插件..."
    kubectl apply -f https://gitee.com/liuqin2014/k8s-shell/raw/master/kube-flannel.yml
    echo "网络插件安装完成。"
}

# 函数：安装 Kubernetes
install_k8s() {
    cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
    sudo yum clean all
    sudo yum makecache
    sudo yum install -y kubelet-v1.32.3 kubeadm-v1.32.3 kubectl-1.v1.32.3
    sudo systemctl enable kubelet
    sudo systemctl start kubelet
}

# 主函数
main() {
    uninstall_docker_and_k8s
    prepare_environment
    install_docker
    install_kubernetes
    init_kubernetes_cluster
    install_network_plugin
    echo "单机部署 Kubernetes 完成。"
}

main
